import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/scheduler.dart';
import 'package:flutterdemo/demos/demo_debug.dart';
import 'package:flutterdemo/demos/demo_hello_world.dart';
import 'package:flutterdemo/demos/demo_route.dart';

import 'demos/demo_base_widget.dart';
import 'demos/demo_counter.dart';

void main() {
  //拦截应用中所有调用print输出日志的行为
  runZoned(() => runApp(MyApp()), zoneSpecification: new ZoneSpecification(
      print: (Zone self, ZoneDelegate parent, Zone zone, String line) {
        parent.print(zone, "Intercepted: $line");
      }),
  );

  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {


    return MaterialApp(
      title: "LJY Flutter Demo",
      theme: ThemeData(
        primarySwatch: Colors.red,
      ),
      home: MyHomePage(title: "LJY Home Page"),
      routes: routers,
    );
  }
}

class MyHomePage extends StatefulWidget {
  final String title;

  MyHomePage({Key key, this.title}) : super(key: key);

  @override
  _MyHomePageState createState() {
    return _MyHomePageState();
  }
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    var routeList = routers.keys.toList();
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: new Container(
        child: new ListView.builder(
          itemBuilder: (context, index) {
            return new InkWell(
              onTap: () {
                Navigator.of(context).pushNamed(routeList[index]);
              },
              child: new Card(
                child: new Container(
                  alignment: Alignment.centerLeft,
                  margin: EdgeInsets.symmetric(horizontal: 10),
                  height: 50,
                  child: new Text(routers.keys.toList()[index]),
                ),
              ),
            );
          },
          itemCount: routers.length,
        ),
      ),
    );
  }
}

Map<String, WidgetBuilder> routers = {
  "hello world": (context) {
    return DemoHelloWorld();
  },
  "计数器示例": (context) {
    return new DemoCounter(
      title: 'DemoCounter',
    );
  },
  "路由管理": (context) {
    return DemoRoute();
  },
  "调试flutter": (context) {
    return DemoDebug();
  },
  "基础widget": (context) {
    return BaseWidget();
  },
};
